package com.atom.common.remote.palace;

import java.io.Serializable;
import java.util.Map;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.Task;

import com.atom.common.CommonConstant;
import com.atom.common.TaskLockStatus;
import com.atom.common.VariablesConstant;
import com.atom.common.util.DateUtil;
import com.atom.common.util.GlobalConfigReader;
import com.atom.common.util.PreConditionUtil;
import com.atom.common.util.UrlGenerateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * Title: RemoteRunningTaskDO
 * Description: 平台待办列表
 * @author WEWE
 * @date 2016年6月28日 下午4:33:26
 * @version 1.0
 */
public class RemoteRunningTaskDO implements Serializable {
  /** serialVersionUID*/
  private static final long serialVersionUID = 1L;

  /** 活动受理人*/
  private String OwnerID;

  /** 被代理人*/
  private String PrincipalID;

  /** 起草人ID*/
  private String CreateUserID;

  /** 起草部门ID*/
  private String CreateDepartID;

  /** 前一步操作人ID*/
  private String PreHandlerID;

  /** 锁定标志*/
  private int Locked;

  /** 任务名称*/
  private String TaskName;

  /** 公文编号*/
  private String DocNum;

  /** 系统模块ID*/
  private String SysModID;

  /** 公文类型ID*/
  private String DocTypeID;

  /** 公文类型*/
  private String DocTypeName;

  /** 工作步骤*/
  private String TaskStepName;

  /** 起始时间*/
  @JsonFormat(pattern = CommonConstant.DATETIME_PATTERN, timezone = CommonConstant.TIMEZONE)
  private String StartTime;

  /** 退回标志*/
  private int Rejected;

  /** 优先级*/
  private String PriorityID;  

  /** 详细信息*/
  private String DetailURL;

  /** 流程图*/
  private String FlowChartURL;

  /** 操作日志*/
  private String OpLogURL;

  /** 超时时间*/
  @JsonFormat(pattern = CommonConstant.DATETIME_PATTERN, timezone = CommonConstant.TIMEZONE)
  private String OverTime;

  /** 主流程实例ID*/
  private String MainProcEntityID;

  /** 流程实例ID*/
  private String ProcEntityID;

  /** 工作步骤ID*/
  private String TaskStepID;

  /** 活动实例ID*/
  private String ActivityEntityID;

  /** 流程版本*/
  private String ProcVersion;

  /** 记录状态*/
  private String Status;

  private String LockUrl;

  public RemoteRunningTaskDO() {
    super();
  }

  public static RemoteRunningTaskDO getRemoteRunningTaskDO(Task task, Map<String, Object> variables, String userId) {
    preCheck(task, variables, userId);
    RemoteRunningTaskDO entity = build(task, variables, userId);
    return entity;
  }

  public static RemoteRunningTaskDO getRemoteRunningTaskDO(DelegateTask task, Map<String, Object> variables, String ownerId, String principalId) {
    preCheck(task, variables, ownerId);
    RemoteRunningTaskDO entity = build(task, variables, ownerId, principalId);
    return entity;
  }

  private static void preCheck(Task task, Map<String, Object> variables, String userId) {
    PreConditionUtil.checkNotNull(task, "task is null");

    PreConditionUtil.checkNotNull(variables, "variables is null");

    PreConditionUtil.checkNotNull(userId, "OwerID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_CREATOR), "CreateUserID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_CREATOR_DEPART), "CreateDepartID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_INSTANCE_NAME), "TaskName is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_NUMBER), "DocNum is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.SYSTEM_MODULER), "SysModID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), "DocTypeID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_DEFINITION_NAME), "DocTypeName is null");

    PreConditionUtil.checkNotNull(task.getName(), "TaskStepName is null");

    PreConditionUtil.checkNotNull(task.getCreateTime(), "StartTime is null");

    PreConditionUtil.checkNotNull(VariablesConstant.IS_REJECT, "isReject is null");

    PreConditionUtil.checkNotNull(VariablesConstant.PRIORITY, "PriorityID is null");

    PreConditionUtil.checkNotNull(task.getTaskDefinitionKey(), "TaskStepID is null");

    PreConditionUtil.checkNotNull(VariablesConstant.MAIN_PROCESS_INSTANCE_ID, "MainProcEntityID is null");

    PreConditionUtil.checkNotNull(task.getId(), "ActivityEntityID is null");

    PreConditionUtil.checkNotNull(task.getProcessDefinitionId(), "ProcEntityID is null");
  }

  private static void preCheck(DelegateTask task, Map<String, Object> variables, String userId) {
    PreConditionUtil.checkNotNull(task, "task is null");

    PreConditionUtil.checkNotNull(variables, "variables is null");

    PreConditionUtil.checkNotNull(userId, "OwerID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_CREATOR), "CreateUserID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_CREATOR_DEPART), "CreateDepartID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_INSTANCE_NAME), "TaskName is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_NUMBER), "DocNum is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.SYSTEM_MODULER), "SysModID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), "DocTypeID is null");

    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_DEFINITION_NAME), "DocTypeName is null");

    PreConditionUtil.checkNotNull(task.getName(), "TaskStepName is null");

    PreConditionUtil.checkNotNull(task.getCreateTime(), "StartTime is null");

    PreConditionUtil.checkNotNull(VariablesConstant.IS_REJECT, "isReject is null");

    PreConditionUtil.checkNotNull(VariablesConstant.PRIORITY, "PriorityID is null");

    PreConditionUtil.checkNotNull(task.getTaskDefinitionKey(), "TaskStepID is null");

    PreConditionUtil.checkNotNull(VariablesConstant.MAIN_PROCESS_INSTANCE_ID, "MainProcEntityID is null");

    PreConditionUtil.checkNotNull(task.getId(), "ActivityEntityID is null");

    PreConditionUtil.checkNotNull(task.getProcessDefinitionId(), "ProcEntityID is null");
  }

  private static RemoteRunningTaskDO build(Task task, Map<String, Object> variables, String userId) {
    RemoteRunningTaskDO entity = new RemoteRunningTaskDO();    

    entity.setOwerID(userId);

    entity.setPrincipalID(task.getOwner());

    entity.setCreateUserID((String) variables.get(VariablesConstant.PROCESS_CREATOR));

    entity.setCreateDepartID((String) variables.get(VariablesConstant.PROCESS_CREATOR_DEPART));

    entity.setPreHandlerID((String) variables.get(VariablesConstant.PREVIOUS_ASSIGNEE));

    if (GlobalConfigReader.getBooleanValue(CommonConstant.LOCK_AUTOMODE)) {
      entity.setLocked((task.getAssignee() == null || task.getAssignee().isEmpty()) ? TaskLockStatus.UNLOCK : TaskLockStatus.LOCK);
    } else {
      entity.setLocked(TaskLockStatus.UNLOCK);
    }    

    entity.setTaskName((String) variables.get(VariablesConstant.PROCESS_INSTANCE_NAME));

    entity.setDocNum((String) variables.get(VariablesConstant.PROCESS_NUMBER));

    entity.setSysModID((String) variables.get(VariablesConstant.SYSTEM_MODULER));

    entity.setDocTypeID((String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY));

    entity.setDocTypeName((String) variables.get(VariablesConstant.PROCESS_DEFINITION_NAME));

    entity.setTaskStepName(task.getName());

    entity.setStartTime(DateUtil.formatTime(task.getCreateTime()));

    entity.setRejected((Boolean) variables.get(VariablesConstant.IS_REJECT) == Boolean.TRUE ? 1 : 0);    

    entity.setPriorityID((String) variables.get(VariablesConstant.PRIORITY));

    entity.setOverTime(DateUtil.formatTime(task.getDueDate()));

    entity.setMainProcEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + (String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID));

    entity.setProcEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + task.getProcessInstanceId());

    entity.setTaskStepID(task.getTaskDefinitionKey());

    entity.setActivityEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + task.getId());

    entity.setDetailURL(UrlGenerateUtil.getDetailUrl(task.getTaskDefinitionKey(), 
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), 
        task.getId()));

    entity.setFlowChartURL(UrlGenerateUtil.getDiagramUrl(task.getTaskDefinitionKey(), 
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), 
        (String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID)));

    entity.setOpLogURL(UrlGenerateUtil.getLogUrl((String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID),
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY)));

    entity.setLockUrl(UrlGenerateUtil.getLockUrl(task.getId(), userId));

    return entity;
  }

  private static RemoteRunningTaskDO build(DelegateTask task, Map<String, Object> variables, String ownerId, String principalId) {
    RemoteRunningTaskDO entity = new RemoteRunningTaskDO();    

    entity.setOwerID(ownerId);

    entity.setPrincipalID(principalId);

    entity.setCreateUserID((String) variables.get(VariablesConstant.PROCESS_CREATOR));

    entity.setCreateDepartID((String) variables.get(VariablesConstant.PROCESS_CREATOR_DEPART));

    entity.setPreHandlerID((String) variables.get(VariablesConstant.PREVIOUS_ASSIGNEE));

    if (GlobalConfigReader.getBooleanValue(CommonConstant.LOCK_AUTOMODE)) {
      entity.setLocked((task.getAssignee() == null || task.getAssignee().isEmpty()) ? TaskLockStatus.UNLOCK : TaskLockStatus.LOCK);
    } else {
      entity.setLocked(TaskLockStatus.UNLOCK);
    }    

    entity.setTaskName((String) variables.get(VariablesConstant.PROCESS_INSTANCE_NAME));

    entity.setDocNum((String) variables.get(VariablesConstant.PROCESS_NUMBER));

    entity.setSysModID((String) variables.get(VariablesConstant.SYSTEM_MODULER));

    entity.setDocTypeID((String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY));

    entity.setDocTypeName((String) variables.get(VariablesConstant.PROCESS_DEFINITION_NAME));

    entity.setTaskStepName(task.getName());

    entity.setStartTime(DateUtil.formatTime(task.getCreateTime()));

    entity.setRejected((Boolean) variables.get(VariablesConstant.IS_REJECT) == Boolean.TRUE ? 1 : 0);    

    entity.setPriorityID((String) variables.get(VariablesConstant.PRIORITY));

    entity.setOverTime(DateUtil.formatTime(task.getDueDate()));

    entity.setMainProcEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + (String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID));

    entity.setProcEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + task.getProcessInstanceId());

    entity.setTaskStepID(task.getTaskDefinitionKey());

    entity.setActivityEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + task.getId());
    
    entity.setDetailURL(UrlGenerateUtil.getDetailUrl(task.getTaskDefinitionKey(), 
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), 
        task.getId()));

    entity.setFlowChartURL(UrlGenerateUtil.getDiagramUrl(task.getTaskDefinitionKey(), 
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), 
        (String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID)));

    entity.setOpLogURL(UrlGenerateUtil.getLogUrl((String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID),
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY)));

    entity.setLockUrl(UrlGenerateUtil.getLockUrl(task.getId(), ownerId));

    return entity;
  }

  public String getOwnerID() {
    return OwnerID;
  }
  public void setOwerID(String ownerID) {
    OwnerID = ownerID;
  }
  public String getPrincipalID() {
    return PrincipalID;
  }
  public void setPrincipalID(String principalID) {
    PrincipalID = principalID;
  }
  public String getCreateUserID() {
    return CreateUserID;
  }
  public void setCreateUserID(String createUserID) {
    CreateUserID = createUserID;
  }
  public String getCreateDepartID() {
    return CreateDepartID;
  }
  public void setCreateDepartID(String createDepartID) {
    CreateDepartID = createDepartID;
  }
  public String getPreHandlerID() {
    return PreHandlerID;
  }
  public void setPreHandlerID(String preHandlerID) {
    PreHandlerID = preHandlerID;
  }
  public String getTaskName() {
    return TaskName;
  }
  public void setTaskName(String taskName) {
    TaskName = taskName;
  }
  public String getDocNum() {
    return DocNum;
  }
  public void setDocNum(String docNum) {
    DocNum = docNum;
  }
  public String getSysModID() {
    return SysModID;
  }
  public void setSysModID(String sysModID) {
    SysModID = sysModID;
  }

  public String getDocTypeID() {
    return DocTypeID;
  }
  public void setDocTypeID(String docTypeID) {
    DocTypeID = docTypeID;
  }
  public String getDocTypeName() {
    return DocTypeName;
  }
  public void setDocTypeName(String docTypeName) {
    DocTypeName = docTypeName;
  }
  public String getTaskStepName() {
    return TaskStepName;
  }
  public void setTaskStepName(String taskStepName) {
    TaskStepName = taskStepName;
  }
  public int getRejected() {
    return Rejected;
  }
  public void setRejected(int rejected) {
    Rejected = rejected;
  }

  public String getDetailURL() {
    return DetailURL;
  }
  public void setDetailURL(String detailURL) {
    DetailURL = detailURL;
  }
  public String getFlowChartURL() {
    return FlowChartURL;
  }
  public void setFlowChartURL(String flowChartURL) {
    FlowChartURL = flowChartURL;
  }
  public String getOpLogURL() {
    return OpLogURL;
  }
  public void setOpLogURL(String opLogURL) {
    OpLogURL = opLogURL;
  }
  public String getMainProcEntityID() {
    return MainProcEntityID;
  }
  public void setMainProcEntityID(String mainProcEntityID) {
    MainProcEntityID = mainProcEntityID;
  }
  public String getProcEntityID() {
    return ProcEntityID;
  }
  public void setProcEntityID(String procEntityID) {
    ProcEntityID = procEntityID;
  }
  public String getTaskStepID() {
    return TaskStepID;
  }
  public void setTaskStepID(String taskStepID) {
    TaskStepID = taskStepID;
  }
  public String getActivityEntityID() {
    return ActivityEntityID;
  }
  public void setActivityEntityID(String activityEntityID) {
    ActivityEntityID = activityEntityID;
  }
  public String getProcVersion() {
    return ProcVersion;
  }
  public void setProcVersion(String procVersion) {
    ProcVersion = procVersion;
  }
  public int getLocked() {
    return Locked;
  }
  public void setLocked(int locked) {
    Locked = locked;
  }
  public String getStartTime() {
    return StartTime;
  }
  public void setStartTime(String startTime) {
    StartTime = startTime;
  }
  public String getPriorityID() {
    return PriorityID;
  }
  public void setPriorityID(String priorityID) {
    PriorityID = priorityID;
  }
  public String getOverTime() {
    return OverTime;
  }
  public void setOverTime(String overTime) {
    OverTime = overTime;
  }
  public String getStatus() {
    return Status;
  }
  public void setStatus(String status) {
    Status = status;
  }
  public String getLockUrl() {
    return LockUrl;
  }
  public void setLockUrl(String lockUrl) {
    LockUrl = lockUrl;
  }

}
